package com.yonyou.iuap.controller.pc;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.KmCommentEntity;
import com.yonyou.iuap.shell.entity.KmKnowledgeStatisticsEntity;
import com.yonyou.iuap.shell.entity.exp.KmCommentEntityVo;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmCommentService;
import com.yonyou.iuap.shell.service.intf.IKnowledgeStatisticsService;

/**
 * 评论
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/pc/comment")
public class PCKmCommentController {

	@Autowired
	private IKmCommentService iKmCommentService;
	@Autowired
	private IKnowledgeStatisticsService iknowledgeStatisticsService;
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	/**
	 * 获取等待回复评论
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getNotCommentList", method = RequestMethod.POST)
	@ResponseBody
	public String getNotCommentList(@RequestBody Map map,HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		int pageNum = Integer.valueOf(map.get("pageNum").toString());
		int pageSize = Integer.valueOf(map.get("pageSize").toString());
		if (pageNum == 0)
			pageNum++;
		if (pageSize == 0)
			pageSize = 15;
		pageNum = (pageNum - 1) * pageSize;
		RequestDto red = new RequestDto();
		red.setPageNum(pageNum);
		red.setPageSize(pageSize);
		red.setPk_user(map.get("pk_user").toString());
		red.setOrderKey(map.get("orderKey").toString());
		red.setOrderRule(map.get("orderRule").toString());

		List<KmCommentEntityVo> dateList = iKmCommentService.getNotCommentList(red);

		ResultDto rsDto = new ResultDto();
		rsDto.setCount(iKmCommentService.getNotCommentCount(red));
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setNum(dateList == null ? 0 : dateList.size());
		rsDto.setData(dateList);
		end=System.currentTimeMillis();
		logger.info("getNotCommentList接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);

	}

	// 评论查询接口
	@RequestMapping(value = "/queryComment", method = RequestMethod.POST)
	public @ResponseBody String queryComment(
			@RequestBody HashMap<String, Object> parMap,
			HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		ResultDto resultDto = iKmCommentService.queryComment(parMap);
//		统计
		KmKnowledgeStatisticsEntity bean=new KmKnowledgeStatisticsEntity();
		bean.setPK_USER((String) parMap.get("pk_user"));
		bean.setPK_KNOWLEDGE((String) parMap.get("pk_publish"));
		bean.setPK_KNOWLEDGE_STATISTICS(IDFactory.getInstance().getID());
		bean.setOPEN_TIME(new Date());
		bean.setPLATFORM("pc");
		iknowledgeStatisticsService.insertSelective(bean);
		
		end=System.currentTimeMillis();
		logger.info("queryComment接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(resultDto);
	}

	// 给知识点添加评论
	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
	public @ResponseBody String addComment(@RequestBody Map parMap,
			HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		KmCommentEntity entity = new KmCommentEntity();
		// 评论内容
		String comment_contant = parMap.get("comment_contant").toString();
		entity.setComment_contant(comment_contant);
		// 评论人
		String pk_user = parMap.get("pk_user").toString();
		entity.setPk_user(pk_user);
		// 评论知识点pk
		String pk_publish = parMap.get("pk_publish").toString();
		entity.setPk_publish(pk_publish);
		// 知识点类型0表示对知识点的评论，1表示对评论的回复
		entity.setComment_type(0);
		// 非空判断
		if (!StringUtils.isEmpty(comment_contant)
				&& !StringUtils.isEmpty(pk_user)
				&& !StringUtils.isEmpty(pk_publish)) {
			ResultDto resultDto = iKmCommentService.insert(entity);
			end=System.currentTimeMillis();
			logger.info("addComment接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		} else {
			ResultDto resultDto = new ResultDto();
			resultDto.setStatus(1);
			resultDto.setMessage("参数不能为空");
			end=System.currentTimeMillis();
			logger.info("addComment接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}

	}

	// 评论回复接口
	@RequestMapping(value = "/answer", method = RequestMethod.POST)
	public @ResponseBody String answer(@RequestBody Map parMap,
			HttpServletRequest request, HttpServletResponse response) {
		start=System.currentTimeMillis();
		KmCommentEntity entity = new KmCommentEntity();
		// 回复内容
		String comment_contant = parMap.get("comment_contant").toString();
		entity.setComment_contant(comment_contant);
		// 回复人
		String pk_user = parMap.get("pk_user").toString();
		entity.setPk_user(pk_user);
		// 回复所在知识点pk
		String pk_publish = parMap.get("pk_publish").toString();
		entity.setPk_publish(pk_publish);
		// 所回复评论的pk
		String pk_parent_comment = parMap.get("pk_parent_comment").toString();
		entity.setPk_parent_comment(pk_parent_comment);
		// 所回复评论的发布者
		String pk_parent_user = parMap.get("pk_parent_user").toString();
		entity.setPk_parent_user(pk_parent_user);
		// 知识点类型0表示对知识点的评论，1表示对评论的回复
		entity.setComment_type(1);
		// 非空判断
		if (!StringUtils.isEmpty(comment_contant)
				&& !StringUtils.isEmpty(pk_user)
				&& !StringUtils.isEmpty(pk_publish)
				&& !StringUtils.isEmpty(pk_parent_comment)
				&& !StringUtils.isEmpty(pk_parent_user)) {
			ResultDto resultDto = iKmCommentService.insert(entity);
			end=System.currentTimeMillis();
			logger.info("answer接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		} else {
			ResultDto resultDto = new ResultDto();
			resultDto.setStatus(1);
			resultDto.setMessage("参数不能为空");
			end=System.currentTimeMillis();
			logger.info("answer接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}
	}
}
